package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Dictionary;
import com.yurun.dictionarymodule.service.DictionaryService;
import com.yurun.loginmodule.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.math.BigInteger;

@Api(tags = "字典模块")
@RestController
@RequestMapping("/dictionaryManage")

public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @ApiOperation(value = "获取职位字典")
    @RequestMapping("/getPositionOption")
    public CommonResult getPositionOption(ServletRequest request, Dictionary dictionary) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        dictionary.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        dictionary.setDictionaryName("position");
        return dictionaryService.getPositionOption(dictionary);
    }

    @ApiOperation(value = "获取所有字典")
    @RequiresPermissions("/getDictionaryList")
    @RequestMapping("/getDictionaryList")
    public CommonResult getDictionaryList(ServletRequest request, String keyword, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        return dictionaryService.getDictionaryList(BigInteger.valueOf(Long.parseLong(organizationId)),BigInteger.valueOf(Long.parseLong(branchId)), keyword, pageNum, pageSize);
    }
}
